#include <stdio.h>

int main()
{
	int factorial[10]={1,1,2,6,24,120,720,5040,40320,362880};
	int max = factorial[9]*7, i, sum = 0, factSum=0, n;	
	for (i = 12; i<=max; i++) {
		n = i;
		factSum = 0;
		while (n>0) {
			factSum += factorial[n%10];
			n /= 10;
		}
		if (factSum == i) sum += i;
	}
	printf("%d\n", sum);
}
